package com.mairuide.inspectionReport.utils;

import com.mairuide.bill.utils.DateUtils;
import com.mairuide.inspectionReport.MailList;
import com.mairuide.phone.PhoneRecord;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Auther: zyj
 * @Date: 2019/6/21 15:23
 * @Description:
 */
public class RuiHaiJsoupUtils {

    /**
     * 抓取手机通讯录
     * @param path
     * @throws IOException
     */
    public static List<MailList> phone(String path,String url,Integer isdel,String uid) throws IOException {
        String name = url.split("#")[1];
        String pageNumber = null;
        pageNumber = path+url.split("#")[0];
        File in = new File(pageNumber);
        Document document = Jsoup.parse(in, "UTF-8");
        Element a = document.selectFirst("[name="+name+"]");
        Element table = a.parent().parent();
        Elements trs = table.select("tr");
        List<MailList> list = new ArrayList<>();
        for (int i=1;i<trs.size();i++){
            if (i%8==2){
                Elements tds = trs.get(i).select("tr");
                MailList mailList = new MailList();
                mailList.setIsDelete(isdel);
                mailList.setContactsName(tds.get(1).text());
                mailList.setContactsMobile(tds.get(3).text());
                mailList.setCasemanageId(uid);

                list.add(mailList);
            }
        }
        String page = url.split("#")[0].split("\\.")[0].replace("Main","");
        Integer p = Integer.parseInt(page);
        while (true){
            p++;
            pageNumber = path+"Main"+String.valueOf(p)+".html";
            in = new File(pageNumber);
            document = Jsoup.parse(in, "UTF-8");
            a = document.selectFirst("[name="+name+"]");
            if (a==null)break;
            table = a.parent().parent();
            trs = table.select("tr");
            for (int i=1;i<trs.size();i++){
                if (i%8==2){
                    Elements tds = trs.get(i).select("tr");
                    MailList mailList = new MailList();
                    mailList.setIsDelete(isdel);
                    mailList.setCasemanageId(uid);
                    mailList.setContactsName(tds.get(1).text());
                    mailList.setContactsMobile(tds.get(3).text());
                    list.add(mailList);
                }
            }
        }
        return list;
    }

    /**
     * 瑞海通话记录
     * @param path
     * @param url
     * @param calltype
     * @return
     * @throws IOException
     */
    public static List<PhoneRecord> callRecord(String path,String url,Integer calltype,String uid) throws IOException {
        String name = url.split("#")[1];
        String pageNumber = null;
        pageNumber = path+url.split("#")[0];
        File in = new File(pageNumber);
        Document document = Jsoup.parse(in, "UTF-8");
        Element a = document.selectFirst("[name="+name+"]");
        Element table = a.parent().parent();
        Elements trs = table.select("tr");
        List<PhoneRecord> list = new ArrayList<>();
        for (int i=1;i<trs.size();i++){
            PhoneRecord pr = new PhoneRecord();
            pr.setCallType(calltype);
            pr.setCasemanageId(uid);
            if (i%8==2){
                Elements tds = trs.get(i).select("td");
                pr.setPartyUserInformation(tds.get(1).text());
                pr.setPartyNumber(tds.get(3).text());
            }else if (i%8==5){//通话时长(秒)
                Element td = trs.get(i).select("td").get(1);
                Date date = DateUtils.getDate(td.text());
                if (date!=null)pr.setStartingTime(new Timestamp(date.getTime()));
            }else if (i%8==6){//通话时长
                String text = trs.get(i).select("td").get(1).text();
                pr.setTalkTime(text);
            }
            list.add(pr);
        }
        String page = url.split("#")[0].split("\\.")[0].replace("Main","");
        Integer p = Integer.parseInt(page);
        while (true){
            p++;
            pageNumber = path+"Main"+String.valueOf(p)+".html";
            in = new File(pageNumber);
            document = Jsoup.parse(in, "UTF-8");
            a = document.selectFirst("[name="+name+"]");
            if (a==null)break;
            table = a.parent().parent();
            trs = table.select("tr");
            for (int i=1;i<trs.size();i++){
                PhoneRecord pr = new PhoneRecord();
                pr.setCallType(calltype);
                pr.setCasemanageId(uid);
                if (i%8==2){
                    Elements tds = trs.get(i).select("td");
                    pr.setPartyUserInformation(tds.get(1).text());
                    pr.setPartyNumber(tds.get(3).text());
                }else if (i%8==5){//通话时长(秒)
                    Element td = trs.get(i).select("td").get(1);
                    Date date = DateUtils.getDate(td.text());
                    if (date!=null)pr.setStartingTime(new Timestamp(date.getTime()));
                }else if (i%8==6){//通话时长
                    String text = trs.get(i).select("td").get(1).text();
                    pr.setTalkTime(text);
                }
                list.add(pr);
            }
        }
        return list;
    }


}
